﻿var BloggerManagerViewModel = function() {
    var self = this;
    self.entities = ko.observableArray();
    self.inProgress = ko.observable();
    self.widgetDisplayMode = ko.observable('viewer');
    self.widgetSize = ko.observable('medium');
    self.selectedEntity = ko.observable();
    self.selectedEntity.extend({ notify: 'always' });
    self.increment = 0;

    self.setNewBlog = function (parameter, selectedEntity, newTemplate) {
        parameter(newTemplate);

    }

    self.populate = function (content, element) {
        self.apiController = new ApiController(this, "Blog", "Blog");
        self.apiController.populate(null, false, content);
        self.apiController.addEntity(self.setNewBlog, self.inProgress, self.selectedEntity().BlogTemplate, self.selectedEntity().Blogs)
        self.apiController.bindElement(element);
        self.toggleUI();
    }

    self.saveTemplate = function(entity)
    {
        self.apiController.update(null, null, self.inProgress(), self.apiController.addToEntities, false);
    }

    self.toggleUI = function(override)
    {
        if (override != null && (override == "editor" || override == "viewer"))
        {
            self.widgetDisplayMode(override);
        }
        else if (self.inProgress().Id() <= 0) {
            self.widgetDisplayMode('editor');
        }
        else {
            if (self.widgetDisplayMode() == "editor") {
                self.widgetDisplayMode('viewer');
            }
            else {
                self.widgetDisplayMode('editor');
            }
        }
    }

    self.openBlog = function (entity)
    {
        self.inProgress(entity);
        self.toggleUI('viewer');
    }

    self.getInProgressEntities = function()
    {
        return self.selectedEntity().Blogs;
    }

    self.addToInProgressEntities = function (entity) {
        var found = false;
        var entities = self.getInProgressEntities();
        for (var i = 0; i < entities().length; i++)
        {
            if (entities()[i].Id() == entity.Id())
            {
                self.position = i;
                entities.setAt(i, entity);
                found = true;
                break;
            }
        }

        if (!found)
        {
            entities.push(entity);
        }

        self.inProgress(entity);
    }

    this.saveInProgress = function()
    {
        self.apiController.update(null, null, self.inProgress(), self.addToInProgressEntities, false);
    }
    this.addBlog = function()
    {
        self.increment--;
        var newJS = ko.mapping.toJSON(self.selectedEntity().BlogTemplate);
        var entity = ko.mapping.fromJSON(newJS);
        entity.Id(self.increment);
        entity.BlogId(self.increment);
        self.selectedEntity().Blogs.push(entity);
        self.inProgress(entity);
        self.toggleUI();
    }

    this.submit = function (entity) {
        //var ApiController = new ApiController(this, "Blog", "Blog");

        self.apiController.update(self.inProgress());
    }
}

